<?php

namespace App\Http\Middleware;

use Closure;
use App\Http\Lib\Output;
use App\Http\Lib\ErrorCode;
use App\Http\Lib\UserLib;

class OAuth2
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {
        if ( \Session::get('uid') ) {
            $ue = UserLib::getLoginUser();           
            if(!$ue) return response()->json(Output::e(ErrorCode::ACCESS_TIMEOUT));
            return $next($request);
        } else {
            //开发用设置登录用户,测试环境和正式环境注释掉
//             if ( env('APP_ENV') !== 'product' ) {
//                 $ue = UserLib::setLoginUser();
//                 return $next($request);
//             }
            //
            if ( $role == 'page' ) {
                return redirect('/login');
            } else {
                exit(json_encode(Output::e(ErrorCode::ACCESS_TIMEOUT)));
            }
        }
    }

}
